import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	ManyToOne,
	JoinColumn,
} from "typeorm";
import { Product } from "./product.entity";

// 使用接口定义而不是直接导入实体类
interface IProductSpec {
	id: number;
	name: string;
	values: string[];
	productId: number;
}

@Entity("product_skus")
export class ProductSku {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ type: "json" })
	specs: Record<string, string>;

	@Column()
	price: number;

	// 实际库存数量
	@Column()
	stock: number;

	// 可用库存数量（下单时扣减）
	@Column({ default: 0 })
	availableStock: number;

	@Column({ nullable: true })
	image: string;

	@ManyToOne(() => Product, (product) => product.skus, { onDelete: "CASCADE" })
	@JoinColumn({ name: "product_id" })
	product: Product;

	@Column()
	productId: number;

	@ManyToOne("ProductSpec", "skus")
	@JoinColumn({ name: "spec_id" })
	spec: IProductSpec;

	@Column({ nullable: true })
	specId: number;
}
